package com.nbsaas.boot.nbtask.task;

import jakarta.jms.JMSException;
import org.apache.activemq.artemis.api.core.client.ClientSession;
import org.apache.activemq.artemis.api.core.management.QueueControl;
import org.apache.activemq.artemis.api.core.management.ResourceNames;
import org.apache.activemq.artemis.core.server.embedded.EmbeddedActiveMQ;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.config.JmsListenerEndpointRegistry;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Service;


@Service
public class QueueMonitorService {

    @Autowired
    private EmbeddedActiveMQ embeddedActiveMQ;

    /**
     * 获取指定队列的消息数量
     *
     * @param queueName 队列名称
     * @return 队列中的消息数量
     */
    public long getQueueSize(String queueName) {
        var managementService = embeddedActiveMQ.getActiveMQServer().getManagementService();
        String queueResourceName = ResourceNames.QUEUE + queueName;


        // 获取 QueueControl 以查询队列状态
        QueueControl queueControl = (QueueControl) managementService.getResource(queueResourceName);
        if (queueControl == null) {
            throw new IllegalArgumentException("Queue " + queueName + " does not exist");
        }
        return queueControl.getMessageCount();
    }

}
